//图文宝添加物流
var twbLogisticVm = new Vue({
    el: '#twbLogisticApp',
    data: {
        title:'添加物流',
        twbLogisticVisible:false,
        /* 订单 */
        orderInfo: {},
        /* 订单区域路径 */
        regionPath: null,
        /* 商品 */
        orderItemList: [],
        /* 商户 */
        sellerInfo:{},
        /* 物流 */
        erpLogistic:{},
        /* 物流方式 */
        logisticsTypeList:[],
        /* 省市区数据 */
        regionOptions:[],
        /* 收货人表单 */
        receiverFormVisable: false,
        receiverForm: {
            toPeople: null,
            toPhone: null,
            toAreaId: null,
            toArea: null,
            toAddress: null,
            areaCascader: null,
        },
        /* 发货人表单  */
        shipperFormVisable: false,
        shipperForm: {
            fromPeople: null,
            fromPhone: null,
            fromAreaId: null,
            fromArea: null,
            fromAddress: null,
            areaCascader: null,
        },
        /* 校验 */
        rules: {
            logisticsId: [
                { required: true, message: '请选择物流方式', trigger: 'blur' },
            ],
            logiCost: [
                { type: 'number', message: '物流费用必须为数字值', trigger: 'change'}
            ],
            toPeople: [
                { required: true, message: '请输入收货人', trigger: 'blur' },
            ],
            toPhone: [
                { required: true, message: '请输入联系方式', trigger: 'blur' },
            ],
            toAreaId: [
                { required: true, message: '请选择收货地址', trigger: 'blur' },
            ],
        },
        /* 快递模板 */
        expressTemplate: 1,

    },
    methods: {
        loadData:function(data){
            this.getInfo(data.orderNo);
        },
        // 查询
        getInfo: function (orderNo) {
            var self = this;
            $.ajax({
                type: "GET",
                url: baseURL + "twbOrder/erporder/twbLogisticInfo/" + orderNo,
                contentType: "application/json",
                data: null,
                success: function(r){
                    if(r.code == 0){
                        self.orderInfo = r.orderInfo;
                        self.regionPath = r.regionPath;
                        self.orderItemList = r.erpOrderItemEntityList;
                        self.regionOptions = JSON.parse(r.area);
                        self.logisticsTypeList = r.logisticsTypeList;
                        self.receiverForm.toPeople = r.orderInfo.shipName;
                        self.receiverForm.toPhone = r.orderInfo.shipMobile;
                        self.receiverForm.toAreaId = r.orderInfo.shipAreaId;
                        self.receiverForm.toArea = r.orderInfo.shipArea;
                        self.receiverForm.toAddress = r.orderInfo.shipAddr;
                        // 省市区信息
                        var regionPath = r.regionPath;
                        if (!isBlank(regionPath)){
                            var regionPathStringArr = regionPath.substring(1, regionPath.length - 1).split(",");
                            var regionPathIntArr = regionPathStringArr.map(function(data){
                                return + data;
                            });
                            self.receiverForm.areaCascader = regionPathIntArr;
                        }

                        $.get(baseURL + "supp/sellerinfo/queryShipInfo", function(r){
                            self.sellerInfo = r.sellerInfo;
                            self.shipperForm.fromPeople = r.sellerInfo.name;
                            self.shipperForm.fromPhone = r.sellerInfo.sellerMobile;
                            self.shipperForm.fromAddress = r.sellerInfo.addr;
                            self.shipperForm.fromArea = r.sellerInfo.area;
                            self.shipperForm.fromAreaId = r.sellerInfo.areaId;
                            var path = r.sellerInfo.regionPath;
                            if (!isBlank(path)){
                                var pathStringArr = path.substring(1, path.length - 1).split(",");
                                var pathIntArr = pathStringArr.map(function(data){
                                    return + data;
                                });
                                self.shipperForm.areaCascader = pathIntArr;
                            }
                        });
                        self.twbLogisticVisible = true;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        // 保存物流信息
        saveLogistics:function () {
            var erpLogistiValid = false;
            var receiverFormValid = false;
            var self = this;
            self.$refs['erpLogistic'].validate(function (valid) {
                erpLogistiValid = valid;
            });
            self.$refs['receiverForm'].validate(function (valid) {
                if (!valid){
                    self.receiverFormVisable = true;
                }
                receiverFormValid = valid;
            });
            if (!erpLogistiValid || !receiverFormValid){
                return false;
            }

            var orderInfo = self.orderInfo;
            var shipperForm = self.shipperForm;
            var receiverForm = self.receiverForm;
            var erpLogistic = self.erpLogistic;
            var orderItemList = self.orderItemList;
            erpLogistic.orderId = orderInfo.orderNo;
            erpLogistic.orderNo = orderInfo.orderNo;
            erpLogistic.sellerId = orderInfo.thirdMerchantId;
            erpLogistic.fromPeople = shipperForm.fromPeople;
            erpLogistic.fromArea = shipperForm.fromArea;
            erpLogistic.fromAddress = shipperForm.fromAddress;
            erpLogistic.fromPhone = shipperForm.fromPhone;
            erpLogistic.toPeople = receiverForm.toPeople;
            erpLogistic.toAreaId = receiverForm.toAreaId;
            erpLogistic.toArea = receiverForm.toArea;
            erpLogistic.toAddress = receiverForm.toAddress;
            erpLogistic.toPhone = receiverForm.toPhone;
            // 发货类型:1.发票,2.样品,3.订单
            erpLogistic.sendType = 3;
            // 物流类型:1.呼叫快递,2.添加物流,3.自提
            erpLogistic.logiType = 2;
            var logisticsItemList = [];
            for (var e in orderItemList){
                var orderItem = orderItemList[e];
                var logiItem = {
                    orderItemId: orderItem.originalItemId,
                    orderItemName: orderItem.name,
                    quantity: orderItem.nums
                }
                logisticsItemList.push(logiItem);
            }

            var erpLogisticsDto = {
                erpLogisticsEntity: erpLogistic,
                erpLogisticsItemList: logisticsItemList
            }
            $.ajax({
                type: "POST",
                url: baseURL + "logistics/erplogistics/saveOrderLogisticsInfo",
                contentType: "application/json",
                data: JSON.stringify(erpLogisticsDto),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            self.receiverFormVisable = false;
                            self.shipperFormVisable = false;
                            self.twbLogisticVisible = false;
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 物流方式选择
        logiTypeChange: function (val) {
            var self = this;
            for (var i = 0, logisticsType; logisticsType = self.logisticsTypeList[i++];) {
                if (logisticsType.logisticsId == val){
                    self.erpLogistic.logiCompany = logisticsType.logisticsName;
                    break;
                }
            }
        },
        // 收货人地区选择
        receiverFormRegionChange: function (val) {
            var self = this;
            var values = val.join(',').split(',');
            var areaId = values[val.length - 1];
            self.receiverForm.toAreaId = areaId;
            var lableObj = this.getCascaderObj(val, self.regionOptions);
            var lable = lableObj.join(',').replace(new RegExp(',', 'g'), ' ');
            self.receiverForm.toArea = lable;
        },
        // 发货人地区选择
        shipperFormRegionChange: function (val) {
            var self = this;
            var values = val.join(',').split(',');
            var areaId = values[val.length - 1];
            self.shipperForm.fromAreaId = areaId;
            var lableObj = this.getCascaderObj(val, self.regionOptions);
            var lable = lableObj.join(',').replace(new RegExp(',', 'g'), ' ');
            self.shipperForm.fromArea = lable;
        },
        // 获取地区路径
        getCascaderObj: function (val, opt) {
            return val.map(function (value, index, array) {
                for (var itm in opt) {
                    if (opt[itm].value == value) {
                        var label = opt[itm].label;
                        opt = opt[itm].children;
                        return label;
                    }
                }
                return null;
            });
        },
        // 保存收货人信息
        receiverFormSave: function () {
            var self = this;
            var logisticsReceiverDto = self.receiverForm;
            logisticsReceiverDto.orderNo = self.orderInfo.orderNo;
            $.ajax({
                type: "POST",
                url: baseURL + "logistics/erplogistics/saveReceiverInfo",
                contentType: "application/json",
                data: JSON.stringify(logisticsReceiverDto),
                success: function (r) {
                    if (r.code === 0) {
                        self.receiverFormVisable = false;
                        self.getInfo(self.orderInfo.orderNo);
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 取消编辑收货人信息
        receiverFormReset: function () {
            var orderInfo = this.orderInfo;
            var receiverForm = this.receiverForm;
            receiverForm.toPeople = orderInfo.shipName;
            receiverForm.toPhone = orderInfo.shipMobile;
            receiverForm.toAreaId = orderInfo.shipAreaId;
            receiverForm.toArea = orderInfo.shipArea;
            receiverForm.toAddress = orderInfo.shipAddr;
            this.receiverFormVisable = false;
        },
        // 保存发货人信息
        shipperFormSave: function () {
            var self = this;
            var logisticsShipperDto = self.shipperForm;
            logisticsShipperDto.orderNo = self.orderInfo.orderNo;
            $.ajax({
                type: "POST",
                url: baseURL + "logistics/erplogistics/saveShipperInfo",
                contentType: "application/json",
                data: JSON.stringify(logisticsShipperDto),
                success: function (r) {
                    if (r.code === 0) {
                        self.shipperFormVisable = false;
                        self.getInfo(self.orderInfo.orderNo);
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 取消编辑发货人信息
        shipperFormReset: function () {
            var sellerInfo = this.sellerInfo;
            var shipperForm = this.shipperForm;
            shipperForm.fromPeople = sellerInfo.name;
            shipperForm.fromPhone = sellerInfo.sellerMobile;
            shipperForm.fromAddress = sellerInfo.addr;
            shipperForm.fromArea = sellerInfo.area;
            shipperForm.fromAreaId = sellerInfo.areaId;
            this.shipperFormVisable = false;
        },
        // 打印快递单模板
        print: function () {
            var self = this;
            var needSave = 0;
            if (self.receiverFormVisable){
                needSave += 1;
                self.receiverFormSave();
            }
            if (self.shipperFormVisable){
                needSave += 1;
                self.shipperFormSave();
            }
            // 执行保存信息操作，延迟1s打印
            if (needSave > 0){
                setTimeout(function (){
                    self.zyPrintComponent();
                }, 1000);
            }else {
                self.zyPrintComponent();
            }
         },
        zyPrintComponent: function () {
            var orderNo = this.orderInfo.orderNo;
            if (this.expressTemplate == 1){
                // 打印韵达
                this.$refs.zyPrintYunda.getOrderData(orderNo);
            }else if (this.expressTemplate == 3){
                // 打印中通
                this.$refs.zyPrintZhongTong.getOrderData(orderNo);
            }else if (this.expressTemplate == 4){
                // 打印速尔
                this.$refs.zyPrintSuEr.getOrderData(orderNo);
            }
        },
    },
});